﻿'Description:关键过程控制文档管理数据访问类
'Copyright (c) : 通力凯顿（北京）系统集成有限公司
'Writer:tianFW
'create Date:2017-10-11
'Rewriter:
'Rewrite Date:
Imports BusinessRule.IBusiness
Imports BusinessRule.DTO
Imports System.Data.OracleClient
Imports DatabaseAccess.DBUtility
Imports System.Text


Public Class uLMSKeyProcessControlDocBusiness
    Implements uLMSKeyProcessControlDocIBusiness

    ''' <summary>
    ''' 添加关键过程控制文档信息
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function AddKeyProcessControlDocInfo(ByVal dto As KeyProcessControlDocDTO) As Boolean Implements uLMSKeyProcessControlDocIBusiness.AddKeyProcessControlDocInfo
        Dim strsql As String =
            <string>
                INSERT INTO KEYPROCESSCONTROLDOC
	            (KEYPROCESSCONTROLDOCID,KEYPROCESSCONTROLDOCNAME, FACTORYID,
	            CREATEEMPLOYEEID,LOSEEFFICACYDATE,CREATEDATE)
                VALUES
	            (:KEYPROCESSCONTROLDOCID,:KEYPROCESSCONTROLDOCNAME,:FACTORYID,
	            :CREATEEMPLOYEEID,:LOSEEFFICACYDATE,:CREATEDATE)
            </string>
        Dim insertpara(5) As OracleParameter
        insertpara(0) = DatabaseHelper.NewParameter(":KEYPROCESSCONTROLDOCID", dto.KeyProcessControlDocID, OracleType.VarChar)
        insertpara(1) = DatabaseHelper.NewParameter(":KEYPROCESSCONTROLDOCNAME", dto.KeyProcessControlDocName, OracleType.VarChar)
        insertpara(2) = DatabaseHelper.NewParameter(":FACTORYID", dto.FactoryID, OracleType.VarChar)
        insertpara(3) = DatabaseHelper.NewParameter(":CREATEEMPLOYEEID", dto.CreateEmployeeID, OracleType.VarChar)
        insertpara(4) = DatabaseHelper.NewParameter(":LOSEEFFICACYDATE", dto.LoseEfficacyDate, OracleType.DateTime)
        insertpara(5) = DatabaseHelper.NewParameter(":CREATEDATE", dto.CreateDate, OracleType.DateTime)
        If DatabaseHelper.ExecuteSql(strsql, insertpara) > 0 Then
            Return True
        Else
            Return False
        End If
    End Function

    ''' <summary>
    ''' 根据关键控制唯一ID删除关键控制信息
    ''' </summary>
    ''' <param name="KeyProcessControlDocID"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function DelKepprocessControlDoc(ByVal KeyProcessControlDocID As String) As Boolean Implements uLMSKeyProcessControlDocIBusiness.DelKepprocessControlDoc
        Dim strsql As String = String.Format("DELETE FROM KEYPROCESSCONTROLDOC K WHERE K.KEYPROCESSCONTROLDOCID='{0}'", KeyProcessControlDocID)
        If DatabaseHelper.ExecuteSql(strsql) > 0 Then
            Return True
        Else
            Return False
        End If
    End Function

    ''' <summary>
    ''' 查询关键过程控制文档记录
    ''' </summary>
    ''' <param name="para"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function SearchData(ByVal para As Dictionary(Of String, String)) As uMESProcessResult Implements uLMSKeyProcessControlDocIBusiness.SearchData
        Dim strsql As New StringBuilder
        strsql.AppendLine("SELECT KCC.*, EM.FULLNAME FROM KEYPROCESSCONTROLDOC KCC ")
        strsql.AppendLine("LEFT JOIN EMPLOYEE EM ON EM.EMPLOYEEID = KCC.CREATEEMPLOYEEID")
        strsql.AppendFormat(" WHERE KCC.FACTORYID='{0}'", para("FactoryID"))
        If para.Keys.Contains("DocName") Then
            strsql.AppendFormat(" AND KCC.KEYPROCESSCONTROLDOCNAME LIKE '%{0}%'", para("DocName"))
        End If
        If para.Keys.Contains("LoseBeginDate") Then
            strsql.AppendFormat(" AND KCC.LOSEEFFICACYDATE >=TO_DATE('{0}', 'YYYY-MM-DD')", para("LoseBeginDate"))
        End If
        If para.Keys.Contains("LoseEndDate") Then
            strsql.AppendFormat(" AND KCC.LOSEEFFICACYDATE <=TO_DATE('{0}', 'YYYY-MM-DD')", para("LoseEndDate"))
        End If
        If para.Keys.Contains("CreateBeginDate") Then
            strsql.AppendFormat(" AND KCC.CREATEDATE >=TO_DATE('{0}', 'YYYY-MM-DD')", para("CreateBeginDate"))
        End If
        If para.Keys.Contains("CreateEndDate") Then
            strsql.AppendFormat(" AND KCC.CREATEDATE <=TO_DATE('{0}', 'YYYY-MM-DD')", para("CreateEndDate"))
        End If
        Dim paraPage As New uMESPagingDataDTO
        paraPage.CurrentPageIndex = Convert.ToInt32(para("CurrentPageIndex"))
        paraPage.PageSize = Convert.ToInt32(para("PageSize").ToString)
        paraPage.strSQL = strsql.ToString
        SearchData = DatabaseHelper.RunProcedureForPage(paraPage)
    End Function

    ''' <summary>
    ''' 修改关键过程控制文档
    ''' </summary>
    ''' <param name="dto"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function ChangeKeyProcessControlDoc(ByVal dto As KeyProcessControlDocDTO) As Boolean Implements uLMSKeyProcessControlDocIBusiness.ChangeKeyProcessControlDoc
        Dim strsql As New StringBuilder
        strsql.AppendFormat("UPDATE KEYPROCESSCONTROLDOC KCC SET KCC.KEYPROCESSCONTROLDOCNAME='{0}', KCC.LOSEEFFICACYDATE=TO_DATE('{1}','YYYY-MM-DD')", dto.KeyProcessControlDocName, dto.LoseEfficacyDate.ToString("yyyy-MM-dd"))
        strsql.AppendFormat(" WHERE KCC.KEYPROCESSCONTROLDOCID = '{0}'", dto.KeyProcessControlDocID)
        If DatabaseHelper.ExecuteSql(strsql.ToString()) > 0 Then
            Return True
        Else
            Return False
        End If
    End Function

    ''' <summary>
    ''' 获取并返回关键过程文档关联的工序记录条数 
    ''' </summary>
    ''' <param name="KeyProcessControlDocID"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetKeyProcessControlIsGNWorkflowStep(ByVal KeyProcessControlDocID As String) As Boolean Implements uLMSKeyProcessControlDocIBusiness.GetKeyProcessControlIsGNWorkflowStep
        Dim strsql As String = String.Format("SELECT COUNT(*) COUNT_ FROM KEYPROCESSCONTROLDOCSTEP KSP WHERE KSP.KEYPROCESSCONTROLDOCID='{0}'", KeyProcessControlDocID)
        If DatabaseHelper.Query_ReturnDatTable(strsql).Rows(0)("COUNT_") > 0 Then
            Return True
        Else
            Return False
        End If
    End Function
End Class
